package com.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.pojo.*;
import com.reggie.service.ICategoryService;
import com.reggie.service.IDishService;
import com.reggie.service.IOrdersDetailService;
import com.reggie.service.IOrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/statics")
public class StaticsController {
    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private IOrdersDetailService ordersDetailService;
    @Autowired
    private IDishService dishService;
    @Autowired
    private ICategoryService categoryService;

    @GetMapping("/dishCash")
    public R dishCash() {
        List<Orders> ordersList = ordersService.list();
        BigDecimal value = new BigDecimal(0);
        ArrayList<Map> pieList = new ArrayList<>();
        Pie pie = new Pie();
        for (Orders order : ordersList) {
            OrderDetail orderDetail = ordersDetailService.getById(order.getId());

            BigDecimal number = new BigDecimal(orderDetail.getNumber());
            BigDecimal amount = orderDetail.getAmount();
            value = amount.multiply(number);
            Dish dish = dishService.getById(orderDetail.getDishId());
            LambdaQueryWrapper<Category> clqw = new LambdaQueryWrapper<>();
            clqw.eq(Category::getId,dish.getCategoryId()).eq(Category::getType,1);
            List<Category> list = categoryService.list(clqw);
            for (Category category : list) {
                HashMap<Integer, String> map = new HashMap<>();
                String name = category.getName();
                map.put(value.intValue(),name);
                pieList.add(map);
                pie.setDishCategoryCash(pieList);
            }
        }
        for (Orders order : ordersList) {
            OrderDetail orderDetail = ordersDetailService.getById(order.getId());

            BigDecimal number = new BigDecimal(orderDetail.getNumber());
            BigDecimal amount = orderDetail.getAmount();
            value = amount.multiply(number);
            Dish dish = dishService.getById(orderDetail.getDishId());
            LambdaQueryWrapper<Category> clqw = new LambdaQueryWrapper<>();
            clqw.eq(Category::getId,dish.getCategoryId()).eq(Category::getType,0);
            List<Category> list = categoryService.list(clqw);
            for (Category category : list) {
                HashMap<Integer, String> map = new HashMap<>();
                String name = category.getName();
                map.put(value.intValue(),name);
                pieList.add(map);
                pie.setSetmealCash(pieList);
            }
        }


        return R.success(pie);
    }

    @GetMapping("/dishNum")
    public R dishNum() {


        return null;
    }

    @GetMapping("/cashPerMonth")
    public R cashPerMonth() {


        return null;
    }
}
